'''
Created on Feb 20, 2011
Parse the quarterly metrics tebles.
@author: solagna
'''
import ParseCommon
import WikiDownloader.FileManager
fm = WikiDownloader.FileManager.FileManager()

__METRIC_TABLE = 1

def parseQuarterMetric (partner, quarter):
    '''
    '''
    
    metr_file = fm.getMetrFile(partner)
    soup = ParseCommon.soupify(metr_file)

    return ParseCommon.parseColumnYfromTableX(soup, quarter, __METRIC_TABLE)

def getMetricsHeader (partner="Finland"):
    metr_file = fm.getMetrFile(partner)
    soup = ParseCommon.soupify(metr_file)
    
    table = ParseCommon.findTableX(soup, __METRIC_TABLE)
    return ParseCommon.parseTableYColHeader(table) 
    
    
def parseAllQuarterMetric (quarter, addPartner = True):
    '''
    Returns a table, no headers, with the metrics for all partners of a given quarter.
    format: Partner name, qm1, qm2,....
    if addPartner = False, format: qm1, qm2,....
    '''
    
    partners = fm.getMetrPartners() #Get the list of the parteners for the metric pages.
    
    out_table = []
    for part in partners:
        part_row = []
        if addPartner:
            part_row.append(part)
        part_row.extend(parseQuarterMetric(part, 5))
        out_table.append(part_row)
    return out_table

if __name__ == "__main__":
    metric = parseQuarterMetric("France",5)
    for riga in metric:
        print "[%s]"%riga
